package com.itheima.reggie.controlle;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * 菜品管理
 */
@RestController
@RequestMapping("dish")
public class DishController {

    @Autowired
    DishService dishService;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    CacheManager cacheManager;

    /**
     * 新增菜品数据
     * post
     * 返回值string
     * 参数dishdto 因为dish一个实体类装不下 还有口味数据
     */
    @PostMapping
    @CacheEvict(cacheNames = "dishList", allEntries = true)
    public R<String> save(@RequestBody DishDto dishDto) {
        dishService.saveWihsFlavors(dishDto);
        //删除redis 所有dish缓存
        //Set<Object> keys = redisTemplate.keys("dish:*");
        //redisTemplate.delete(keys);
        return R.success("添加成功");
    }

    /**
     * 分页条件查询
     */
    @GetMapping("/page")
    public R<Page<DishDto>> pageDishDto(@RequestParam("page") Integer page,
                                        @RequestParam("pageSize") Integer pageSize,
                                        String name) {
        return R.success(dishService.pageDishDto(page, pageSize, name));
    }

    /**
     * 根据id回显
     */
    @GetMapping("{id}")
    public R<DishDto> findDishDto(@PathVariable Long id) {
        DishDto findDishDto = dishService.getDishDtoById(id);
        return R.success(findDishDto);
    }

    /**
     * 修改菜品
     * 返回值 string
     * 参数传递dishDto 因为有口味数据
     */
    @PutMapping
    @CacheEvict(cacheNames = "dishList", allEntries = true)
    public R<String> update(@RequestBody DishDto dishDto) {
        dishService.updateByDishDto(dishDto);
        //删除redis 所有dish缓存
        //Set<Object> keys = redisTemplate.keys("dish:*");
        //Object keys =  redisTemplate.opsForValue().get("dish:" + dishDto.getCategoryId() + ":" + "null");
        //String key = "dish:" + dishDto.getCategoryId() + ":";, m
        //System.out.println(keys);
        //redisTemplate.delete(keys);
        return R.success("修改成功!");
    }

    /**
     * 批量起售停售
     * 参数一个状态 一个是前端传来的数组id
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status,
                                  @RequestParam("ids") Long[] ids) {
        //调用service层判断
        dishService.update()
                //判断传来的状态 0就修改成0 1就修改成1
                .set("status", status)
                //根据id修改
                .in("id", ids).update();
        return R.success("修改成功");
    }

    /**
     * 删除
     * 参数: ids
     */
    @DeleteMapping
    public R<String> delete(Long[] ids) {
        dishService.deleteByIds(ids);
        return R.success("删除成功");
    }

    /**
     * 查询分类对应的菜品
     * 返回值categoryId  name
     */
    @GetMapping("/list")
    //开启redis存储缓存数据  cacheName=key的前缀   key=key的唯一表示  cacheaNames+key=真正的key
    //result 表示当前方法的返回值
    @Cacheable(cacheNames = "dishList", key = "#categoryId+ ':'+ #name", unless = "#result.data.size()==0")
    public R<List<DishDto>> list(Long categoryId, String name) {
       /* List<DishDto> dish = (List<DishDto>) redisTemplate.opsForValue().get("dish:" + categoryId + ":" + name);
        if (dish != null) {
            return R.success(dish);
        }*/
        List<DishDto> dishList = dishService.listDishDto(categoryId, name);
        //redisTemplate.opsForValue().set("dish:" + categoryId + ":" + name, dishList, 5, TimeUnit.MINUTES);

        return R.success(dishList);
    }


}
